/*
 * SDLGUIEventHandler.h
 *
 *  Created on: Jul 29, 2010
 *      Author: Ry
 */

#ifndef SDLGUIEVENTHANDLER_H_
#define SDLGUIEVENTHANDLER_H_

#include <vector>
#include "GameWindow.h"
#include "gui/GUI.h"
#include "event/SDLEventHandler.h"
#include "gui/GUIEvent.h"

class SDLGUIEventHandler: public SDLEventHandler {
private:
	GUI *gui;
	GameWindow *window;

public:
	SDLGUIEventHandler(GUI *gui, GameWindow *window){
		this->gui = gui;
		this->window = window;
	}

	void handleEvent(float dts) {
		if (event.type == SDL_MOUSEMOTION) {
			std::vector<Component*> search = gui->getComponents();
			std::vector<Component*> nextSearch;

			while (search.size() > 0){
				for (unsigned int i = 0; i < search.size(); i++) {
					Component *component = search[i];
					std::vector<Component*> children = component->getChildren();
					if (component->getRect().inside(event.motion.x, window->getCurrentResolution().height - event.motion.y)) {
						if (!component->getHover()) {
							GUIEvent guiEvent;
							guiEvent.type = GUIEvent::MOUSEENTER;
							component->setHover(true);
							component->handleEvent(&guiEvent);
						}
						nextSearch.insert(nextSearch.end(), children.begin(), children.end());
					} else {
						if (component->getHover()) {
							GUIEvent guiEvent;
							guiEvent.type = GUIEvent::MOUSEEXIT;
							component->setHover(false);
							component->handleEvent(&guiEvent);
							nextSearch.insert(nextSearch.end(), children.begin(), children.end());
						}
					}
				}
				search = nextSearch;
				nextSearch.clear();
			}
		}
	}

	virtual ~SDLGUIEventHandler(){}
};

#endif /* SDLGUIEVENTHANDLER_H_ */
